<template>
    <div v-if="merchantInfo" class="trace-container">
        <!-- 顶栏 -->
        <van-nav-bar title="小票追溯" :fixed="true" :safe-area-inset-top="true" :border="false"
            style="background: #3da010; color: #fff;" />
        <!-- <img src="../../assets/image/bg_top.jpg" alt=""> -->
        <!-- 主内容区 -->
        <main class="content">
            <!-- 宣传图 -->
            <div class="banner-box">
                <img class="banner-img" src="../../assets/image/bg_top.jpg" alt="食品安全宣传图">
            </div>

            <!-- 商户信息 -->
            <section class="merchant-card">
                <div class="market-name">{{ merchantInfo.marketnm }}</div>

                <div class="merchant-info">
                    <van-image @click="showPreview(merchantInfo.pic)" round width="60" height="60"
                        :src="merchantInfo.pic" class="merchant-avatar" />
                    <div class="merchant-detail">
                        <div class="base-info">
                            <span>经营户：{{ merchantInfo.name }}</span>
                            <van-tag type="primary">摊位号:{{ merchantInfo.twhname }}</van-tag>
                        </div>
                        <div class="rating-info">
                            <div class="stars">
                                <van-rate readonly v-model="merchantInfo.xydj" :size="15" color="#ffd21e" void-icon="star"
                                    void-color="#eee" />
                            </div>
                            <div class="counters">
                                <van-icon name="good-job" color="#fff" />点赞{{ merchantInfo.dianzan }}
                                <van-icon name="warning" color="#fff" />投诉{{ merchantInfo.tousu }}
                            </div>
                        </div>
                    </div>
                </div>
            </section>

            <!-- 商品信息 -->
            <section v-for="(item, index) in productInfo" :key="index" class="product-card">
                <div class="b-time"> 购买日期：{{ item.ckTime }}</div>
                <header class="card-header">
                    购买商品：{{ item.name }}
                </header>

                <div class="info-grid">
                    <div class="h_img">
                        <van-image @click="showPreview(item.xpspimg)" width="120" height="120" :src="item.xpspimg" />
                    </div>
                    <div class="info-list">
                        <div class="info-row">
                            <span class="label">交易序号:</span>
                            <span class="value">
                                {{ item.card }}
                            </span>
                        </div>
                        <div class="info-row">
                            <span class="label" style="color: red;">单价:{{ item.danjia }}元/kg</span>
                            <span class="value">
                                重量:{{ item.jhsum }}kg
                            </span>
                        </div>
                        <div class="info-row">
                            <span class="label">检测日期:</span>
                            <span class="value">
                                {{ item.ckTime.slice(0, 10) }}
                            </span>
                        </div>
                        <div class="info-row">
                            <span class="label">检测结果:</span>
                            <span class="value">
                                {{ item.xpjcjg }}合格
                            </span>
                        </div>
                        <div class="info-row">
                            <span class="label">商品来源:</span>
                            <span class="value">
                                {{ item.comeaddress }}
                            </span>
                        </div>
                    </div>
                </div>
            </section>

            <!-- 评价二维码 -->
            <div class="qrcode-box">
                <qrcode-vue style="margin-top: 5px" :value="qrcodeImg" :size="150" level="H" />
                <p class="qrcode-tip">长按对该交易进行评价</p>
                <div class="phone">投诉电话：12315</div>
            </div>
        </main>
    </div>
    <van-loading v-if="loading" color="#0094ff" type="spinner" size="54px">加载中...</van-loading>
</template>

<script setup>
import { getCaptcha, getInfo } from '../../api/home'
import { reactive, ref } from 'vue';
import { showImagePreview } from 'vant';
import QrcodeVue from "qrcode.vue";
const merchantInfo = ref()
const productInfo = ref()
const loading = ref(false)
const qrcodeImg = ref('https://h5qct.yskjtz.com/?sc=t&type=pjsh&id='+localStorage.getItem('id'))
const getInit = () => {
    loading.value = true
    getCaptcha().then(res => {
        console.log(res);
    })
    getInfo({ mark: localStorage.getItem('id') }).then(res => {
        console.log(res);
        merchantInfo.value = res.data.people
        productInfo.value = res.data.jyhspcks
        loading.value = false
    })
}

// 图片预览方法
const showPreview = (url) => {
    showImagePreview([url]);
};

getInit()



</script>

<style lang="scss" scoped>
.trace-container {
    // min-height: 100vh;
    background: #f5f5f5;

    :deep(.van-nav-bar__title) {
        color: #fff;
    }

    .content {
        padding-top: 46px;
    }

    .banner-box {
        position: relative;
        height: 180px;

        .banner-img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .banner-text {
            position: absolute;
            bottom: 20px;
            left: 0;
            right: 0;
            text-align: center;
            color: white;
            font-size: 18px;
            text-shadow: 1px 1px 4px rgba(0, 0, 0, 0.3);
        }
    }

    .merchant-card {
        background: #3da010;
        color: white;
        padding: 16px;
        margin: 12px;
        border-radius: 8px;

        .market-name {
            font-size: 18px;
            margin-bottom: 12px;
        }

        .merchant-info {
            display: flex;
            align-items: center;
            gap: 12px;

            .merchant-detail {
                flex: 1;

                .base-info {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    margin-bottom: 8px;
                }

                .rating-info {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;

                    .counters {
                        font-size: 14px;
                        color: rgba(255, 255, 255, 0.9);

                        .van-icon {
                            margin-right: 4px;
                            vertical-align: middle;
                        }
                    }
                }
            }
        }
    }

    .product-card {
        background: white;
        border-radius: 8px;
        margin: 12px;
        overflow: hidden;

        .b-time {
            padding-bottom: 10px;
            background-color: #f5f5f5;
        }

        .card-header {
            background: linear-gradient(90deg,#393,rgb(116, 151, 9));
            color: white;
            font-weight: 600;
            padding: 10px;
            font-size: 18px;
            text-align: center;
        }

        .info-grid {
            padding: 12px;
            display: flex;
            font-size: 14px;

            .h_img {
                display: flex;
                align-items: center;
            }

            .info-list {
                padding-left: 10px;
                width: calc(100% - 120px);
                display: flex;
                flex-direction: column;
                justify-content: space-between;
            }

            .info-row {
                display: flex;

                .label {
                    text-align: left;
                    width: 45%;
                    color: #333;
                    padding: 3px 0;
                }

                .value {
                    padding: 3px 0;
                    text-align: left;
                    width: 45%;
                    color: #333;
                }
            }
        }
    }

    .qrcode-box {
        min-height:250px;
        margin: 12px;
        overflow: hidden;
        background-color: #fff;
        text-align: center;
        padding-top: 20px;
        padding-bottom: 10px;

        .qrcode-tip {
            color: #999;
            font-size: 16px;
            margin-top: 8px;
        }
        .phone{
            font-size: 18px;
            font-weight: 600;
        }
    }
}

.van-loading {
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}
</style>